//
//  CJExten+UIViewHolder.swift
//  CJPack
//
//  Created by 陈警卫 on 2023/2/7.
//

import UIKit
import Foundation
import SnapKit

/// 全局设置占位视图(自定义视图类名 继承自CJExtenHolder)
public var cj_viewHolderName: String = ""

/// 全局设置占位图
public var cj_defaultViewImageName: String = ""

/// 基类占位图
open class CJExtenHolder: UIView {
    open var cj_data: Any?
}

/// 默认占位图
open class CJExtenHolderView: CJExtenHolder {
    
    open override var cj_data: Any? {
        didSet {
            cj_setHolderData()
        }
    }
    
    open func cj_setHolderData() {
        guard let cj_data = cj_data else { return }
        guard let dict = (cj_data as? Dictionary<String, String>) else { return }
        
        if let imageName = dict["imagename"] {
            if imageName == "cj_view_blank" {
                cj_holderImageView.image = UIImage(named: "cj_view_blank", in: .cj_bundle, compatibleWith: nil)
            } else {
                cj_holderImageView.image = UIImage(named: imageName)
            }
        }
        if let title = dict["title"] {
            cj_holderTitleL.text = title
            cj_holderTitleL.isHidden = false
        } else {
            cj_holderTitleL.isHidden = true
        }
    }
    
    // 主图
    open var cj_holderImageView: UIImageView!
    // 提示语
    open var cj_holderTitleL: UILabel!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = UIColor.white
        
        cj_holderImageView = UIImageView()
        cj_holderImageView.contentMode = .scaleAspectFit
        addSubview(cj_holderImageView)
        
        cj_holderTitleL = UILabel()
        cj_holderTitleL.textAlignment = .center
        cj_holderTitleL.numberOfLines = 0
        cj_holderTitleL.font = UIFont.systemFont(ofSize: 15)
        cj_holderTitleL.textColor = UIColor.cj_color("929292")
        addSubview(cj_holderTitleL)
    }
    
    open override func layoutSubviews() {
        super.layoutSubviews()
        
        cj_holderImageView.frame = CGRect(x: 0, y: cj_height * 0.2,
                                          width: cj_width * 0.3, height: cj_width * 0.3)
        cj_holderImageView.cj_centerX = cj_width * 0.5
        
        cj_holderTitleL.frame = CGRect(x: 10, y: cj_holderImageView.cj_bottom + 20,
                                       width: cj_width - 20, height: 0)
        cj_holderTitleL.cj_height = cj_holderTitleL.cj_heightFits
    }

    required public init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


public extension UIView {
    
    private struct CJViewHolderKey {
        static var cj_holderViewKey: Void?
    }
    
    /// 图片占位图
    var cj_holderView: CJExtenHolder? {
        get {
            return objc_getAssociatedObject(self, &CJViewHolderKey.cj_holderViewKey) as? CJExtenHolder
        }
        set {
            objc_setAssociatedObject(self, &CJViewHolderKey.cj_holderViewKey, newValue, .OBJC_ASSOCIATION_RETAIN)
        }
    }
    
    /// 移除占位图
    func cj_removeHolderView() {
        if let cj_holderView = cj_holderView {
            cj_holderView.removeFromSuperview()
        }
    }
    
    /// 添加占位图
    func cj_setHolderView(_ title: String? = nil, holderImageName: String? = "cj_view_blank") {
        
        if let cj_holderView = cj_holderView {
            if self.subviews.contains(cj_holderView) {
                return
            }
        }
        
        var cj_holderViewClassTemp: AnyClass? = NSClassFromString(cj_viewHolderName)
        if cj_holderViewClassTemp == nil {
            cj_holderViewClassTemp = CJExtenHolderView.self
        }
        
        let cj_holderaViewClass = cj_holderViewClassTemp as! CJExtenHolder.Type
        
        cj_holderView = cj_holderaViewClass.init()
        
        if let cj_holderView = cj_holderView {
            addSubview(cj_holderView)
            cj_holderView.frame = CGRect(x: 0, y: 0, width: self.cj_width, height: self.cj_height)
        }
         
        var dict: [String: String] = [:]
        if let holderImageName = holderImageName {
            dict["imagename"] = holderImageName
        }
        if let title = title {
            dict["title"] = title
        }
        cj_holderView?.cj_data = dict
    }
}
